package com.CGFrame.struts.bean.jobSchedule;

import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;

import com.CGFrame.struts.POJOs.Category;
import com.CGFrame.struts.POJOs.Make;
import com.CGFrame.struts.POJOs.Shot;
import com.CGFrame.struts.POJOs.Shotgroup;
import com.CGFrame.struts.bean.ProjectOperate;
import com.CGFrame.struts.bean.ProjectOperateFactory;
import com.CGFrame.struts.bean.unit.ShotGroupBean;
import com.CGFrame.struts.bean.unit.UnitBean;

public class SSchedule {
	private Shotgroup shotGroup;
	private ProjectOperate pop;
	public SSchedule(String shotGroupId) throws Exception 
	{
		pop = ProjectOperateFactory.getprojectOperate();
		shotGroup = (Shotgroup)pop.UniqueQuery("Shotgroup", "shotGroupId,"+shotGroupId );
		if (shotGroup == null)
		{
			throw new Exception("No The ShotGroup");
		}
		
	}
	public StringBuffer getSGHeader()
	{
		StringBuffer result= new StringBuffer("ID$ShotName$Frames$StartTime$EndTime");
		
		List shotCategories = pop.Query("Category", "company.companyId,"+shotGroup.getProject().getCompany().getCompanyId()+",categoryType,Shot", "categoryId", "asc");
		int size = shotCategories.size();
		for (int i = 0;i<size;i++)
		{
			Category category = (Category)shotCategories.get(i);
			result.append("$"+category.getCategoryName()+"");
		}
		return result;
	}
	
	public LinkedHashSet getShotSchedule()
	{
		LinkedHashSet shotSchedule = new LinkedHashSet();
		ShotGroupBean sGBean = new ShotGroupBean(shotGroup);
		Iterator ite_shots = sGBean.getAllShots().iterator();
		while(ite_shots.hasNext())
		{
			Shot shot = (Shot)ite_shots.next();
			shotSchedule.add(getDateByShot(shot));
		}
		return shotSchedule;
	}
	public static  StringBuffer getDateByShot(Shot shot)
	{
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		StringBuffer result = new StringBuffer(""+shot.getShotId()+"$"+shot.getShotName()+"$"+shot.getShotTotalFrameNum()+"");
		String startTime = "";
		String endTime   = "";
		try
		{
			SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd");
			startTime = dateFm.format(shot.getPlanStartTime());
			endTime   = dateFm.format(shot.getPlanEndTime());
		}
		catch (Exception e)
		{
			// Time is  null 
		}
		result.append("$").append(startTime).append("$").append(endTime);
		UnitBean unitBean = new UnitBean("Shot",shot.getShotId());
		List shotCategories = pop.Query("Category", "company.companyId,"+shot.getShotgroup().getProject().getCompany().getCompanyId()+",categoryType,Shot", "categoryId", "asc");
		int size = shotCategories.size();
		for (int i = 0 ; i<size ; i++)
		{
			Category category = (Category)shotCategories.get(i);
			Make make;
			if ( ( make =unitBean.getMakeByCategory(category) )==null )
			{
				result.append("$ ");
				
			}
			else
			{
				result.append("$"+make.getStatus().getStatusName()+"" );
			}
		}
		return result;
	}
}
